package com.qpl.depression.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qpl.depression.entity.*;
import com.qpl.depression.service.*;
import com.qpl.depression.utils.PdfUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private BookService bookService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryLevelService categoryLevelService;

    /**
     * 跳转到首页面
     * @param modelMap 存放数据的map
     * @return 首页
     */
    @GetMapping("index")
    public String indexPage(ModelMap modelMap) {
        List<Article> articles = articleService.getByRandom();
        modelMap.addAttribute("articles", articles);
        List<Article> randomArticle = articleService.getByRandom();
        modelMap.addAttribute("randomArticle", randomArticle);
        Video video = videoService.list().get(0);
        modelMap.addAttribute("video", video);
        Book book = bookService.selectOne();
        modelMap.addAttribute("book", book);
        List<Category> category = categoryService.getByRandom();
        modelMap.addAttribute("category", category);
        return "index";
    }

    @GetMapping("download/pdf")
    public void pdf(HttpServletResponse response, Long categoryLevelId, double scope, int paperSize, int time) throws Exception {
        CategoryLevel categoryLevel = categoryLevelService.getById(categoryLevelId);
        PdfUtils.ExportPdf(response, categoryLevel, scope, paperSize, time);
    }

}
